package com.chen.controller;

import com.chen.dao.UserMapper;
import com.chen.dto.PageData;
import com.chen.dto.ResponData;
import com.chen.entity.User;
import com.chen.general.Constans;
import com.chen.general.StateCode;
import com.chen.service.FanService;
import com.chen.until.DownLoadExcelUntils;
import com.chen.until.MyUntils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("api/excel")
public class ExcelDownLoadController {


    @Autowired
    private FanService fanService;

    @RequestMapping("downLoadMyFans")
    public ResponData getExcel(HttpServletRequest request, HttpServletResponse response,
                               @RequestParam(name = "currentPage", defaultValue = "0") int pageNum,
                               @RequestParam(name = "pageSize", defaultValue = "0") int pageSize,
                               @RequestParam("uid") String uid,
                               @RequestParam(name = "fjbh", required = false) String fjbh,
                               @RequestParam(name = "projectid") String projectid,
                               @RequestParam(name = "btime", required = false) String btime,
                               @RequestParam(name = "etime", required = false) String etime) throws IOException {
        PageData pageData = fanService.queryFansForDownExcel(pageNum, pageSize, fjbh, projectid, btime, etime, uid);
        if(pageData.getData() == null || pageData.getData().size() == 0 ){
            return new ResponData(Constans.DATA_ERROR, StateCode.DATA_ERROR);
        }else{
            List<LinkedHashMap> fansData = pageData.getData();
            String[] headers = {"序号", "所属项目", "风机编号", "并网时间", "首检时间",
                    "上次全检年", "预计下次全年检", "上次半年检", "预计下次半年检", "上次巡检", "预计下次巡检", "备注"};
            DownLoadExcelUntils.downLoadExcel(request, response, fansData, headers, "myfans_");
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        }
    }

    @RequestMapping("downLoadYjyqFans")
    public ResponData downLoadYjyqFans(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "currentPage", defaultValue = "0") int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "0") int pageSize,
            @RequestParam(name = "projectid") String pid,
            @RequestParam(name = "type", required = false, defaultValue = "") String type) throws IOException {
        String queryStr = "";
        if (!"".equals(type)) {
            String beginStr = " AND (";
            String endStr = " ) ";
            if (type.contains("0")) {
                //要看全年检
                queryStr += " NOT (yuqi_mes.qnjyqsj = '未逾期')  OR";
            }
            if (type.contains("1")) {
                //要看半年检
                queryStr += " NOT (yuqi_mes.bnjyqsj = '未逾期')  OR";
            }
            if (type.contains("2")) {
                //要看巡检
                queryStr += " NOT (yuqi_mes.xjyqsj = '未逾期')  OR";
            }
            queryStr = queryStr.substring(0, queryStr.length() - 2);
            queryStr = beginStr + queryStr + endStr;
        }
        PageData pageData = fanService.queryYqFansByPidForDownExcel(pageNum, pageSize, pid, queryStr);
        if(pageData.getData() == null || pageData.getData().size() == 0 ){
            return new ResponData(Constans.DATA_ERROR, StateCode.DATA_ERROR);
        }else{
            List<LinkedHashMap> fansData = pageData.getData();
            String[] headers = {"序号", "所属项目", "风机编号","预期全年检时间","逾期天数(全)","预期半年检时间","逾期天数(半)","预期巡检时间","逾期天数(巡)"};
            DownLoadExcelUntils.downLoadExcel(request, response, fansData, headers, "yqfans_");
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        }
    }




    @RequestMapping("downLoadJjyqFans")
    public ResponData downLoadJjyqFans(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "currentPage", defaultValue = "0") int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "0") int pageSize,
            @RequestParam(name = "projectid") String projectid,
            @RequestParam(name = "duration", required = false, defaultValue = "") String duration,
            @RequestParam(name = "type", defaultValue = "", required = false) String type
    ) throws IOException {
        String queryStr1 = "";
        if (!"".equals(duration)) {
            queryStr1 = "AND ( jijiangyuqi_mes.jqnjdqsj BETWEEN 1 AND " + duration
                    + " OR jijiangyuqi_mes.jbnjdqsj BETWEEN 1 AND " + duration
                    + " OR jijiangyuqi_mes.jxjdqsj BETWEEN 1  AND " + duration
                    +"  OR  SUBSTR( jijiangyuqi_mes.jqnjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期' " +
                    "   OR  SUBSTR( jijiangyuqi_mes.jbnjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期' " +
                    "   OR  SUBSTR( jijiangyuqi_mes.jxjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期' "
                    + ")";
        }

        String queryStr2 = "";
        if (!"".equals(type) &&  !"".equals(duration)) {
            String beginStr = " AND (";
            String endStr = " OR  SUBSTR( jijiangyuqi_mes.jqnjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期' " +
                    "  OR  SUBSTR( jijiangyuqi_mes.jbnjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期' " +
                    "  OR  SUBSTR( jijiangyuqi_mes.jxjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期'   ) ";
            if (type.contains("0")) {
                //要看全年检
                queryStr2 += "   jijiangyuqi_mes.jqnjdqsj BETWEEN 1 AND " + duration  +  " OR";
            }
            if (type.contains("1")) {
                //要看半年检
                queryStr2 += "  jijiangyuqi_mes.jbnjdqsj BETWEEN 1 AND " + duration +  " OR";
            }
            if (type.contains("2")) {
                //要看巡检
                queryStr2 += " jijiangyuqi_mes.jxjdqsj BETWEEN 1 AND " + duration +  "  OR";
            }
            queryStr2 = queryStr2.substring(0, queryStr2.length() - 2);
            queryStr2 = beginStr + queryStr2 + endStr;

        }

        PageData pageData = this.fanService.queryJjYqFansForDownExcel(pageNum, pageSize, projectid, queryStr1,queryStr2);
        if(pageData.getData() == null || pageData.getData().size() == 0 ){
            return new ResponData(Constans.DATA_ERROR, StateCode.DATA_ERROR);
        }else{
            List<LinkedHashMap> fansData = pageData.getData();
            String[] headers = {"序号", "所属项目", "风机编号","预期全年检时间","即将逾期(全)","预期半年检时间","即将逾期(半)","预期巡检时间","即将逾期(巡)"};
            DownLoadExcelUntils.downLoadExcel(request, response, fansData, headers, "jjyqfans_");
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        }
    }














}
